<?php

/* Pass in by reference! */
function ganglia_graph_cpu_report ( &$rrdtool_graph ) {

    global $ganglia_context,
           $ganglia_cpu_idle_color,
           $ganglia_cpu_nice_color,
           $ganglia_cpu_system_color,
           $ganglia_cpu_user_color,
           $ganglia_cpu_wio_color,
           $ganglia_hostname,
           $ganglia_range,
           $ganglia_rrd_dir,
           $ganglia_size,
           $ganglia_strip_domainname;

    if ($ganglia_strip_domainname) {
       $ganglia_hostname = ganglia_strip_domainname($ganglia_hostname);
    }

    $rrdtool_graph['height'] += ($ganglia_size == 'medium') ? 14 : 0;
    $title = 'CPU';
    if ($ganglia_context != 'host') {
       $rrdtool_graph['title'] = $title;
    } else {
       $rrdtool_graph['title'] = "$ganglia_hostname $title last $ganglia_range";
    }
    $rrdtool_graph['upper-limit']    = '100';
    $rrdtool_graph['lower-limit']    = '0';
    $rrdtool_graph['vertical-label'] = 'Percent';
    $rrdtool_graph['extras']         = '--rigid';

    if($ganglia_context != "host" ) {

        /*
         * If we are not in a host context, then we need to calculate
         * the average
         */
        $series =
              "DEF:'num_nodes'='${ganglia_rrd_dir}/cpu_user.rrd':'num':AVERAGE "
            . "DEF:'cpu_user'='${ganglia_rrd_dir}/cpu_user.rrd':'sum':AVERAGE "
            . "CDEF:'ccpu_user'=cpu_user,num_nodes,/ "
            . "DEF:'cpu_nice'='${ganglia_rrd_dir}/cpu_nice.rrd':'sum':AVERAGE "
            . "CDEF:'ccpu_nice'=cpu_nice,num_nodes,/ "
            . "DEF:'cpu_system'='${ganglia_rrd_dir}/cpu_system.rrd':'sum':AVERAGE "
            . "CDEF:'ccpu_system'=cpu_system,num_nodes,/ "
            . "DEF:'cpu_idle'='${ganglia_rrd_dir}/cpu_idle.rrd':'sum':AVERAGE "
            . "CDEF:'ccpu_idle'=cpu_idle,num_nodes,/ "
            . "AREA:'ccpu_user'#$ganglia_cpu_user_color:'User CPU' "
            . "STACK:'ccpu_nice'#$ganglia_cpu_nice_color:'Nice CPU' "
            . "STACK:'ccpu_system'#$ganglia_cpu_system_color:'System CPU' ";

        if (file_exists("$ganglia_rrd_dir/cpu_wio.rrd")) {
            $series .= "DEF:'cpu_wio'='${ganglia_rrd_dir}/cpu_wio.rrd':'sum':AVERAGE "
                ."CDEF:'ccpu_wio'=cpu_wio,num_nodes,/ "
                ."STACK:'ccpu_wio'#$ganglia_cpu_wio_color:'WAIT CPU' ";
        }

        $series .= "STACK:'ccpu_idle'#$ganglia_cpu_idle_color:'Idle CPU' ";

    } else {

        /* Context is not "host" */

        $series ="DEF:'cpu_user'='${ganglia_rrd_dir}/cpu_user.rrd':'sum':AVERAGE "
        ."DEF:'cpu_nice'='${ganglia_rrd_dir}/cpu_nice.rrd':'sum':AVERAGE "
        ."DEF:'cpu_system'='${ganglia_rrd_dir}/cpu_system.rrd':'sum':AVERAGE "
        ."DEF:'cpu_idle'='${ganglia_rrd_dir}/cpu_idle.rrd':'sum':AVERAGE "
        ."AREA:'cpu_user'#$ganglia_cpu_user_color:'User CPU' "
        ."STACK:'cpu_nice'#$ganglia_cpu_nice_color:'Nice CPU' "
        ."STACK:'cpu_system'#$ganglia_cpu_system_color:'System CPU' ";

        if (file_exists("$ganglia_rrd_dir/cpu_wio.rrd")) {
            $series .= "DEF:'cpu_wio'='${ganglia_rrd_dir}/cpu_wio.rrd':'sum':AVERAGE ";
            $series .= "STACK:'cpu_wio'#$ganglia_cpu_wio_color:'WAIT CPU' ";
        }

        $series .= "STACK:'cpu_idle'#$ganglia_cpu_idle_color:'Idle CPU' ";
    }

    $rrdtool_graph['series'] = $series;

    return $rrdtool_graph;
}


